<?php
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';
class UserPermissionsDao extends BaseDao
{
    protected $_name = 'user_permissions';
    private $_joinTableName = 'permissions';
    private $_joinTableName1 = 'admins';
    public function getPermissionsByUserId ($admin_id, $permission = null)
    {
        $db = $this->db_r;
        $select = $db->select();
        $select->from($this->_name, '');
        $select->join($this->_joinTableName1, $this->_name . ".user_id=$this->_joinTableName1.id", '');
        $select->join($this->_joinTableName, $this->_joinTableName . ".id=$this->_name.permission_id", array('id' , 'description' , 'resource_id' , 'operate_id'));
        $select->distinct();
        if ($permission) {
            foreach ($permission as $row) {
                $select->where("$this->_joinTableName.id!=?", $row['id']);
            }
        }
        $select->where("$this->_joinTableName1.id=?", $admin_id);
        $select->where(" $this->_name.is_delete=0 ");
        $sql = $select->__toString();
        return $db->fetchAll($sql);
    }
    public function deletePermission ($user_id)
    {
        $db = $this->db_w;
        $where = $db->quoteInto('user_id = ?', $user_id);
        return $db->delete($this->_name, $where);
       
    }
    
}
